<template>
  <div class="searchWrapper">
    <div class="input" :class="{edit: model == 2}">
      <SearchOutlined :style="{ fontSize: '13px' }" />
      <input :value="condition" :placeholder="placeholderMsg" @focus="searchHandler(1)" @input="onChangeValue"/>
      <CloseCircleOutlined v-if="route.name === 'users' && searchStatus===1 && model===0" class="close" @click="searchHandler(0)"/>
    </div>
    <template v-if="route.name === 'comments'">
      <div class="op">
        <PlusOutlined />
      </div>
    </template>
    <template v-if="route.name === 'users'">
      <div v-if="model==2" class="op cancel" @click="doSearchBtn(0)">取消</div>
      <div v-if="model==0" class="op" @click="doSearchBtn(2)"><UserAddOutlined /></div>
    </template>
  </div>
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue'
import debounce from '@/utils/debounceUtil'
import Pubsub from 'pubsub-js'
import { SearchOutlined, PlusOutlined, UserAddOutlined, CloseCircleOutlined } from '@ant-design/icons-vue'
import { RouterLink, useRoute } from 'vue-router'
const placeholderMsg = ref("搜索")
const condition = ref("")
const route = useRoute()
const model = ref<number>(0) // 0是正常模式 搜索列表 1是搜索好友 2是搜索用户
const searchStatus = ref<number>(0) // 0是正常状态 1是搜索状态

// 文字发生变化
const onChangeValue = (e: any) => {
  Pubsub.publish('search-users-value', e.target.value)
}


// 向好友页面发送消息
const doSearchBtn = (type: number) => {
  model.value = type
  if(route.name === 'users') { // 正常状态
    Pubsub.publish('filter-users-model', type)
  }
}

const searchHandler = (type: number) => {
  if(model.value === 2) return
  searchStatus.value = type
  if(route.name === 'users') { // 正常状态
    Pubsub.publish('filter-users-model', type)
  }
}

// 搜索
const doSearch = () => {
  console.log('开始传递搜索结果')
}

</script>
<style lang="less" scoped>
.searchWrapper {
  background-color: white;
  height: 65px;
  display: flex;
  justify-content: space-between;
  flex-direction: row;
  align-items: center;
  padding-top: 10px;
  box-sizing: border-box;
  border-bottom: 1px solid #e2e2e2;
  .input {
    background-color: hsl(0, 0%, 90%);
    flex: 1;
    margin-left: 10px;
    margin-right: 10px;
    display: flex;
    height: 26px;
    align-items: center;
    padding-left: 2%;
    border-radius: 3px;
    border: 1px solid #e2e2e2;
    &.edit {
      background-color: white;
      input {
        background-color: white;
      }
    }
    input {
      background-color: hsl(0, 0%, 90%);
      outline: none;
      border: none;
      width: 100%;
      margin-left: 5px;
      font-size: 12px;
      height: 24px;
    }
    .close {
      font-size: 13px;
      color: #9e9e9e;
      margin-right: 7px;
    }
  }
  .op {
    height: 26px;
    width: 26px;
    background-color: #f0f0f0;
    border-radius: 2px;
    margin-right: 10px;
    text-align: center;
    line-height: 25px;
  }
  .cancel {
    background-color: unset;
    color: #147542;
    font-size: 12px;
  }
}
</style>
